#pragma once
#include "CoreMinimal.h"
#include "Windows/WindowsApplication.h"
#include "./ThirdParty/include/Common.h"
#include <chrono>

DECLARE_LOG_CATEGORY_EXTERN(UIMessager, Log, All);

class CPPPROJECT_API CUIMessager : public IWindowsMessageHandler
{
public:
	CUIMessager();
	virtual ~CUIMessager();

	virtual bool ProcessMessage(HWND Hwnd, uint32 Message, WPARAM WParam, LPARAM LParam, int32& OutResult) override;
	void sendStartMessage(APlayerController* vPlayerController);
	void sendEndMessage();

private:
	void __processDefaultMessage(const std::chrono::duration<double>& vDuration, uint32 Message, WPARAM WParam, LPARAM LParam);
	void __processWMINPUTMessage(WPARAM WParam, LPARAM LParam, const std::chrono::duration<double>& vDuration, uint32 Message);
	void __processMouseButtonMessage(const std::chrono::duration<double>& vDuration, uint32 Message, WPARAM WParam, LPARAM LParam, HWND Hwnd);
	void __processMouseMoveMessage(const std::chrono::duration<double>& vDuration, uint32 Message, WPARAM WParam, LPARAM LParam, HWND Hwnd);
	void __sendMessage(const MessageProcess::SMessage& vMessage);

	bool m_IsFirstMessage = true;
	bool m_IsReplaying = false;
	long m_MessageID = 0;
    int m_ReceivedMsgId = 0;
	std::chrono::time_point<std::chrono::steady_clock> m_StartTimePoint;
	std::chrono::time_point<std::chrono::steady_clock> m_CurrentTimePoint;

	HWND m_HwndReceiver = NULL;
	HANDLE m_SharedMemoryHandle = NULL;
	LPVOID m_SharedMemory = NULL;
	HANDLE m_FirstSenderSemaphore = NULL;
	HANDLE m_FirstReceiverSemaphore = NULL;
	HANDLE m_SecondSenderSemaphore = NULL;
	HANDLE m_SecondReceiverSemaphore = NULL;
	MessageProcess::SMessage* m_Buffer1 = NULL;
	MessageProcess::SMessage* m_Buffer2 = NULL;
	MessageProcess::SMessage* m_TargetBuffer = NULL;
	APlayerController* m_PlayerController = NULL;

	int m_BufferIndex = 0;
	MessageProcess::SMessage* m_pShareMemoryData = nullptr;
	TMap<uint32_t, FString> m_MessageMap;
};
